//
//  AppreciationCollection.m
//  Lovelife
//
//  Created by dllo on 15/4/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AppreciationCollection.h"
#import "AppercitionTCell.h"
#import "AppreciationModel.h"
#import "UIImageView+WebCache.h"
@interface AppreciationCollection ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *mytableView;
@property(nonatomic, retain)NSMutableArray *manegerArr;
@property(nonatomic, copy)NSString *path;
@end

@implementation AppreciationCollection
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject] stringByAppendingPathComponent:@"appreclation.plist"];
        NSLog(@"%@",self.path);
        self.manegerArr = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithFile:self.path]];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    [self tableView];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)tableView
{
    self.mytableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
    [self.view addSubview:_mytableView];
    [_mytableView release];
}

//重写编辑的方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.mytableView setEditing:editing animated:editing];
}
//控制那些行可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//删除状态下的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"dele";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.manegerArr removeObjectAtIndex:indexPath.row];
        [self.mytableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
        [NSKeyedArchiver archiveRootObject:self.manegerArr toFile:_path];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _manegerArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse;
    AppercitionTCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[AppercitionTCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];
    }
    AppreciationModel *model = self.manegerArr[indexPath.row];
    cell.titleLabel.text = model.title;
    NSURL *url = [NSURL URLWithString:model.url];
    [cell.photoView sd_setImageWithURL:url];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [_mytableView release];
    [_manegerArr release];
    [super dealloc];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
